package chat.server.statemachine.termin;

import java.io.IOException;

import chat.State;
import chat.server.ChatServerElectionState;
import chat.server.ChatServerState;
import chat.statemachine.AbstractContent;
import chat.statemachine.AlgorithmActionInvocationException;

public class AlgorithmTerminActions {
	static boolean debug = true;
	/**
	 * Function called when received a terminaison token
	 * @param state
	 * @param content
	 * @return
	 * @throws AlgorithmActionInvocationException
	 */
	public static Object receiveTerminTokenContent(final State state,
			final AbstractContent content)
	throws AlgorithmActionInvocationException {
		ChatServerState sstate = null;
		if (content == null) {
			throw new IllegalArgumentException(
			"Try executing action with null content");
		}
		if (state == null) {
			throw new IllegalArgumentException(
			"Try executing action with null state");
		}
		if (state instanceof ChatServerState) {
			sstate = (ChatServerState) state;
		} else {
			throw new IllegalArgumentException(
					"Try executing action with state of wrong type ("
					+ state.getClass().getName()
					+ " instead of ChatServerState");
		}
		if (content instanceof TerminTokenContent) {
			/* upon reception of a token <toc, c, q> */
			TerminTokenContent ttc = (TerminTokenContent) content;
			if (debug) { System.out.println("Received terminaison token, color :" + ttc.getColor() + "\tq:" + ttc.getQ()); }
			if (sstate.state == ChatServerElectionState.Winner) {
				/* p == P0 */
				if ((ttc.getColor() == TerminColor.WHITE)
						&& (sstate.color == TerminColor.WHITE)
						&& ((sstate.mc + ttc.getQ()) == 0)
						&& (sstate.server.getNumOfClients() == 0)) {
					/* c == white && color_p == white && mc_p + q = 0 */
					/* && nombre de clients connectés = 0 */
					/* condition ajoutée */
					System.out.println("Announce");
				}
				else {
					/* Send <tok, white, 0> to Next */
					/* Handled by seperate Thread */
					sstate.TerminSemaphore.release();
				}
			}
			else {
				/* p != P0 */
				if ((sstate.color == TerminColor.WHITE) && (sstate.server.getNumOfClients() == 0)) {
					/* color_p == white */
					try {
						TerminTokenContent ttcsend = new TerminTokenContent(ttc.getColor(),ttc.getQ() + sstate.mc);
						sstate.server.sendToAServer(sstate.getNext(), AlgorithmTermin.TERMIN_TOKEN_MESSAGE.getActionIndex(), ttcsend);
						/* send <tok, c, q + mc> to Next */
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				else {
					/* color_p != white */
					try {
						TerminTokenContent ttcsend = new TerminTokenContent(TerminColor.BLACK,ttc.getQ() + sstate.mc);
						sstate.server.sendToAServer(sstate.getNext(), AlgorithmTermin.TERMIN_TOKEN_MESSAGE.getActionIndex(), ttcsend);
						/* send <tok, BLACK, q + mc> to Next */
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			sstate.color = TerminColor.WHITE;
			/* color_p = white */
		} else {
			throw new AlgorithmActionInvocationException(
					"Error when executing action: not right message type ("
					+ content.getClass().getName() + ")");
		}
		return null;
	}
}
